package mapReduce.homework.q6_orderInfoTop10;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class OrderInfoWritable implements WritableComparable<OrderInfoWritable> {
    private String year = "";
    private String orderId = "";
    private String partner = "";
    private String menuId = "";
    private double money;

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getPartner() {
        return partner;
    }

    public void setPartner(String partner) {
        this.partner = partner;
    }

    public String getmenuId() {
        return menuId;
    }

    public void setmenuId(String menuId) {
        this.menuId = menuId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public int compareTo(OrderInfoWritable o) {
        int res = 0;

        /*
            1.按年份升序排序
            2.年份相同按合作方升序排序
            3.合作方相同按套餐名称升序排序，
            4.如果套餐名称相同，按金额倒序排序
            5.如果金额相同，按订单编号升序排序
         */
        if(year.equals(o.getYear())){
            if(partner.equals(o.getPartner())){
                if(menuId.equals(o.getmenuId())){
                    if(money==o.getMoney()){
                        res = orderId.compareTo(o.getOrderId());
                    }else{
                        res = (int)(-(money-o.getMoney()));
                    }
                }else{
                    res = menuId.compareTo(o.getmenuId());
                }
            }else{
                res = partner.compareTo(o.partner);
            }
        }else{
            res = year.compareTo(o.getYear());
        }

        return res;
    }

    public void write(DataOutput out) throws IOException {
        out.writeUTF(year);
        out.writeUTF(orderId);
        out.writeUTF(partner);
        out.writeUTF(menuId);
        out.writeDouble(money);
    }

    public void readFields(DataInput in) throws IOException {
        this.year = in.readUTF();
        this.orderId = in.readUTF();
        this.partner = in.readUTF();
        this.menuId = in.readUTF();
        this.money = in.readDouble();
    }

    @Override
    public String toString() {
        return year+"\t"+orderId+"\t"+partner+"\t"+menuId+"\t"+money;
    }
}
